package org.freetek.linkdn.util.communication;

import java.text.MessageFormat;

import org.freetek.linkdn.util.model.LinkedInAuthResponse;
import org.freetek.linkdn.util.model.LinkedInUpdatesResponse;
import org.freetek.linkdn.util.scribe.ScribeUtil;
import org.scribe.http.Request;
import org.scribe.http.Response;
import org.scribe.http.Request.Verb;
import org.scribe.oauth.Scribe;
import org.scribe.oauth.Token;

public class LinkedInUpdatesWrapper {

	private static final String HTTP_REQUEST_LINKEDIN_UPDATES = "http://api.linkedin.com/v1/people/~/network?count={0}";

	public LinkedInUpdatesResponse getUpdates(LinkedInAuthResponse authResponse, String verifier) {
		Token authToken = new Token(authResponse.getRequestToken(), authResponse.getRequestTokenSecret());

		Scribe scribe = ScribeUtil.getScribeInstance();
		Token accessToken = scribe.getAccessToken(authToken, verifier);

		Request request = new Request(Verb.GET, MessageFormat.format(HTTP_REQUEST_LINKEDIN_UPDATES, 50));
		Token updatesToken = new Token(accessToken.getToken(), accessToken.getSecret());

		scribe.signRequest(request, updatesToken);
		Response response = request.send();

		return new LinkedInUpdatesResponse(response.getBody());
	}
}
